//
//  LYCommitOrderViewController.m
//  artist
//
//  Created by Jay_Apple on 16/6/5.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYCommitOrderViewController.h"
#import "LYVideoModel.h"
#import "LYIntegrationModel.h"
#import "LYOrderDetailModel.h"
#import "LYOrderDetailViewController.h"

@interface LYCommitOrderViewController ()

@property (weak, nonatomic) IBOutlet UILabel *name;

@property (weak, nonatomic) IBOutlet UILabel *address;


@property (weak, nonatomic) IBOutlet UILabel *originPrice;

@property (weak, nonatomic) IBOutlet UILabel *integrationMessage;

@property (weak, nonatomic) IBOutlet UILabel *realPrice;

@property (assign, nonatomic) BOOL use_integral;

@property (strong, nonatomic) LYSubjectDetailModel *subModel;

@property (strong, nonatomic) LYIntegrationModel *integrationModel;

@end

@implementation LYCommitOrderViewController

- (void)viewDidLoad {

    
    [super viewDidLoad];
    
    self.navigationItem.title = @"提交订单";
    
    [self loadOrderData];
    
}

#pragma mark - 重写方法
- (void)setSubModel:(LYSubjectDetailModel *)subModel{

    _subModel = subModel;
    
    //课程名称
    self.name.text = [NSString stringWithFormat:@"课程名称：%@",_subModel.title];
    
    //地址
    NSString *address = [NSString stringWithFormat:@"上课地点：%@%@%@%@",_subModel.province,_subModel.city,_subModel.area,_subModel.address];
    self.address.text = address;
    //价钱
    self.originPrice.text = [NSString stringWithFormat:@"¥%@",_subModel.price];
    self.realPrice.text = self.originPrice.text;

}

- (void)setIntegrationModel:(LYIntegrationModel *)integrationModel{
    
    _integrationModel = integrationModel;
    
    self.integrationMessage.text = [NSString stringWithFormat:@"积分：%zd  可抵扣：%.2f¥",_integrationModel.integral,_integrationModel.deduct_money];

}

- (void)setUse_integral:(BOOL)use_integral{

    _use_integral = use_integral;
    
    if (_use_integral) {
        
        CGFloat realPrice = [self.subModel.price doubleValue] - self.integrationModel.deduct_money;
        if (realPrice < 0) {
            realPrice = 0;
        }
        self.realPrice.text = [NSString stringWithFormat:@"¥%.2f",realPrice];
    }else{
        
        self.realPrice.text = self.originPrice.text;
        
    }

}

#pragma mark - 网络请求


- (void)loadOrderData{

    [MBProgressHUD showMessage:nil];
    [LYRequestTool getSubjectDeatilWithToken:LYToken course_id:self.course_id success:^(LYSubjectDetailModel *model) {
        
        self.subModel = model;
        [self loadIntegrationData];
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
        
    }];

}

- (void)loadIntegrationData{

    [LYRequestTool getCanuseIntegrationWithToken:LYToken success:^(LYIntegrationModel *model) {
        
        self.integrationModel = model;
        [MBProgressHUD hideHUD];
        
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];

}

- (void)commitOrder{


    CGFloat reduceMoney = self.use_integral ? self.integrationModel.deduct_money : 0;
    NSString *reduce = [NSString stringWithFormat:@"%.2f",reduceMoney];
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool orderAddWithToken:LYToken course_id:self.course_id use_integral:reduce success:^(LYOrderAddModel *orderModel) {
        
        [MBProgressHUD hideHUD];
        
        LYOrderDetailViewController *orderVC = [[LYOrderDetailViewController alloc] init];
        orderVC.order_sn = orderModel.order_sn;
        [self.navigationController pushViewController:orderVC animated:YES];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:message];
    }];


}

#pragma mark - 按钮点击

- (IBAction)useIntegrationOnlcick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.use_integral = sender.selected;
}

- (IBAction)commitOrderOnclcik:(id)sender {
    
    [self commitOrder];

}


@end
